/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.tools.adtui.model.axis

import com.android.tools.adtui.model.AspectObserver
import com.android.tools.adtui.model.Range
import com.android.tools.adtui.model.formatter.TimeAxisFormatter
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Test

class ResizingAxisComponentModelTest {
  private lateinit var range: Range
  private lateinit var globalRange: Range
  private lateinit var axisModel: ResizingAxisComponentModel

  @Before
  fun setUp() {
    range = Range(6.0, 10.0)
    globalRange = Range(5.0, 20.0)
    axisModel = ResizingAxisComponentModel.Builder(range, TimeAxisFormatter.DEFAULT).setGlobalRange(globalRange).build()
  }

  @Test
  fun `reacts to global range min change`() {
    val observer = AspectObserver()

    var aspectFired = false
    axisModel.addDependency(observer).onChange(AxisComponentModel.Aspect.AXIS) {
      aspectFired = true
    }

    assertThat(aspectFired).isFalse()
    globalRange.set(2.0, 3.0)
    assertThat(aspectFired).isTrue()
  }

  @Test
  fun `ignores when global range min does not change`() {
    val observer = AspectObserver()

    var aspectFired = false
    axisModel.addDependency(observer).onChange(AxisComponentModel.Aspect.AXIS) {
      aspectFired = true
    }

    assertThat(aspectFired).isFalse()
    globalRange.set(5.0, 5.0)
    assertThat(aspectFired).isFalse()
  }

  @Test
  fun `reacts to range change`() {
    val observer = AspectObserver()

    var aspectFired = false
    axisModel.addDependency(observer).onChange(AxisComponentModel.Aspect.AXIS) {
      aspectFired = true
    }

    assertThat(aspectFired).isFalse()
    range.set(8.0, 8.0)
    assertThat(aspectFired).isTrue()
  }
}
